<?php


namespace app\controller\api;


use app\BaseController;
use think\facade\Db;

use app\models\user\PromotionRewardsModel;

class FinanceController extends BaseController
{
    /**
     * 推广订单
     */
    public function getPromoteOrder()
    {
        try {
            $userId = $this->request->userId;
            // 直属下级
            $userIdArr = Db::name('user')->where('inviter', $userId)->column('id');
            $where = $whereOr = [];
            $where[] = ['is_del', '=', 0];
            $where[] = ['is_pick', '=', 0];
            $where[] = ['update_time', '>=', date('Y-m-d 00:00:00')];
            $type = $this->request->param('type', 0);
            $page = $this->page;
            $limit = $this->limit;
            if ($type == 0) {
                // 全部
                $where[] = ['user_id', 'in', $userIdArr];
            } elseif ($type == 1) {
                // 待付款
                $where[] = ['order_status', '=', 1];
                $where[] = ['user_id', 'in', $userIdArr];
            } elseif ($type == 2) {
                // 待收款
                $where[] = ['order_status', '=', 2];
                $where[] = ['user_id', 'in', $userIdArr];
            } elseif ($type == 3) {
                // 待上架
                $where[] = ['order_status', '=', 4];
                $where[] = ['user_id', 'in', $userIdArr];
            } else {
                // 已出
                $where[] = ['order_status', '=', 3];
                $where[] = ['user_id', 'in', $userIdArr];
            }
            $list = Db::name('order')
                ->where($where)
                ->order('id', 'desc')
                ->paginate(['list_rows' => $limit, 'page' => $page])
                ->toArray();
            if ($list['total']) {
                $goodsIdArr = array_column($list['data'], 'goods_id');
                $goodsList = Db::name('goods')->whereIn('id', $goodsIdArr)->column('goods_no,name,image', 'id');
                $userIdArr1 = array_column($list['data'], 'user_id');
                $userIdArr2 = array_column($list['data'], 'buy_user_id');
                $userIdArr3 = array_column($list['data'], 'sell_user_id');
                $userIdArr = array_unique(array_merge($userIdArr1, $userIdArr2, $userIdArr3));
                $userList = Db::name('user')->whereIn('id', $userIdArr)->column('user_name,mobile,invite_code', 'id');
                foreach ($list['data'] as &$v) {
                    $goodsId = $v['goods_id'];
                    $v['goods_no'] = $goodsList[$goodsId]['goods_no'] ?? '';
                    $v['goods_name'] = $goodsList[$goodsId]['name'] ?? "";
                    $v['goods_image'] = $goodsList[$goodsId]['image'] ?? "";
                    $sellUserId = $v['user_id'];
                    $buyUserId = $v['buy_user_id'];
                    $v['sell_user_name'] = $userList[$sellUserId]['user_name'] ?? '';
                    $v['sell_user_mobile'] = $userList[$sellUserId]['mobile'] ?? '';
                    $v['sell_user_code'] = $userList[$sellUserId]['invite_code'] ?? '';
                    $v['buy_user_name'] = $userList[$buyUserId]['user_name'] ?? '';
                    $v['buy_user_mobile'] = $userList[$buyUserId]['mobile'] ?? '';
                    $v['buy_user_code'] = $userList[$buyUserId]['invite_code'] ?? '';
                }
            }
            $totalAmount = Db::name('order')
                ->whereIn('user_id', $userIdArr)
                ->where('update_time', '>=', date('Y-m-d 00:00:00'))
                ->sum('price');
            return $this->success(['list' => $list, 'total_amount' => $totalAmount, 'count' => $list['total']]);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 业绩统计明细
     */
    public function getPerformanceOrder()
    {
        try {
            $userId = $this->request->userId;
            $paintId = $this->request->paintId;
            // 直属下级
            $userIdArr = Db::name('user')->where('relation', 'like', '%' . $userId . '%')->where('id', '<>', $userId)->column('id');
            $where = $whereOr = [];
            $where[] = ['is_del', '=', 0];
            $where[] = ['is_pick', '=', 0];
            $where[] = ['paint_id', '=', $paintId];
            $where[] = ['update_time', '>=', date('Y-m-d 00:00:00')];
            $type = $this->request->param('type', 0);
            $page = $this->page;
            $limit = $this->limit;
            if ($type == 0) {
                // 全部
//                $where[] = ['user_id', 'in', $userIdArr];
            } elseif ($type == 1) {
                // 待付款
                $where[] = ['order_status', '=', 1];
//                $where[] = ['user_id', 'in', $userIdArr];
            } elseif ($type == 2) {
                // 待收款
                $where[] = ['order_status', '=', 2];
//                $where[] = ['user_id', 'in', $userIdArr];
            } elseif ($type == 3) {
                // 待上架
                $where[] = ['order_status', '=', 4];
//                $where[] = ['user_id', 'in', $userIdArr];
            } else {
                // 已出
                $where[] = ['order_status', '=', 3];
//                $where[] = ['user_id', 'in', $userIdArr];
            }
            $list = Db::name('order')
                ->where($where)
                ->order('id', 'desc')
                ->paginate(['list_rows' => $limit, 'page' => $page])
                ->toArray();
            if ($list['total']) {
                $goodsIdArr = array_column($list['data'], 'goods_id');
                $goodsList = Db::name('goods')->whereIn('id', $goodsIdArr)->column('goods_no,name,image', 'id');
                $userIdArr1 = array_column($list['data'], 'user_id');
                $userIdArr2 = array_column($list['data'], 'buy_user_id');
                $userIdArr3 = array_column($list['data'], 'sell_user_id');
                $userIdArr = array_unique(array_merge($userIdArr1, $userIdArr2, $userIdArr3));
                $userList = Db::name('user')->whereIn('id', $userIdArr)->column('user_name,mobile,invite_code', 'id');
                foreach ($list['data'] as &$v) {
                    $goodsId = $v['goods_id'];
                    $v['goods_no'] = $goodsList[$goodsId]['goods_no'] ?? '';
                    $v['goods_name'] = $goodsList[$goodsId]['name'] ?? "";
                    $v['goods_image'] = $goodsList[$goodsId]['image'] ?? "";
                    $sellUserId = $v['user_id'];
                    $buyUserId = $v['buy_user_id'];
                    $v['sell_user_name'] = $userList[$sellUserId]['user_name'] ?? '';
                    $v['sell_user_mobile'] = $userList[$sellUserId]['mobile'] ?? '';
                    $v['sell_user_code'] = $userList[$sellUserId]['invite_code'] ?? '';
                    $v['buy_user_name'] = $userList[$buyUserId]['user_name'] ?? '';
                    $v['buy_user_mobile'] = $userList[$buyUserId]['mobile'] ?? '';
                    $v['buy_user_code'] = $userList[$buyUserId]['invite_code'] ?? '';
                }
            }
            $totalAmount = Db::name('order')
//                ->whereIn('user_id', $userIdArr)
                ->where($where)
                ->sum('price');
            return $this->success(['list' => $list, 'total_amount' => $totalAmount, 'count' => $list['total']]);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }


    /**
     * 申请推荐奖励记录
     * @return \think\response\Json
     */
    public function getPromotionRewardsList()
    {
        try {
            $where = [];
            $name = $this->request->param('user_name', '', 'trim');
            if ($name) {
                $userIdArr = UserModel::where('nick|mobile', 'like', '%' . $name . '%')->column('id');
                $where[] = ['user_id', 'in', $userIdArr];
            }
            $type = $this->request->param('type', '', 'trim');
            if ($type) {
                $where[] = ['type', '=', $type];
            }
            $status = $this->request->param('status', '', 'trim');
            if ($status != "") {
                $where[] = ['status', '=', $status];
            }
            $startTime = $this->request->param('start_time', '', 'trim');
            if ($startTime) {
                $where[] = ['create_time', '>=', strtotime($startTime)];
            }
            $endTime = $this->request->param('end_time', '', 'trim');
            if ($endTime) {
                $where[] = ['create_time', '<=', strtotime($endTime)];
            }
            $list = PromotionRewardsModel::where($where)
                ->order('id', 'desc')
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as &$v) {
                $userInfo = UserModel::find($v['user_id']);
                $v['user_name'] = $userInfo['nick'] ?? '';
                $v['mobile'] = $userInfo['mobile'] ?? "";
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}